/*
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-23 09:45:29
 * @LastEditTime: 2023-02-17 15:21:43
 */

import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'
import dt from '@dt/dt'
import channelTypeMgr from '@/biz/option/channelType'
import planOpt from '@/biz/option/plan.js'

let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
    {
      title: '',
      minWidth: 50,
      type: 'checkbox'
    },
    {
      title: '认购单编号',
      field: 'orderCode',
      minWidth: 170,
      header: {
        filter: {
          title: '认购单编号',
          key: 'orderCode',
          placeholder: '请输入至少4位认购单编号',
          input: {},
          check: (val) => {
            if (val.orderCode.length < 4) {
              _this.$Message.warning('请输入至少4位认购单编号。')
            }
            return !(val.orderCode.length < 4)
          }
        },
      },
      slots: {
        default: (params, h) => {
          if (params.row.isBreakLowPrice == 1) {
            return table.cell.text(h, {
              value: params.row.orderCode + '(底)'
            })
          } else {
            return table.cell.text(h, {
              value: params.row.orderCode
            })
          }
        }
      }
    },
    {
      title: '认购状态',
      field: 'showStatus',
      minWidth: 130,
      cell: (item) => {
        return {
          text: {
            type: 'dict',
            key: 'subscriptionStatus',
            color: item.showStatus == 2 ? '#D9001B' : '#515a6e'
          }
        }
      }
    },
    {
      field: 'counselorName',
      minWidth: 130,
      header: {
        filter: nameOpt.createCounselorPeople({
          key: 'counselorId',
          title: '归属顾问',
          to: (info) => {
            return {
              counselorId: info.userId
            }
          }
        })
      }
    },
    {
      field: 'customerName',
      minWidth: 150,
      title: '客户姓名',
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: params.row.customerName,
            event: 'onLinkUser',
            data: params.row
          })
        }
      },
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {},
        },
      }
    },
    {
      title: '客户手机号',
      field: 'customerPhone',
      minWidth: 150,
      header: {
        filter: {
          title: '客户手机号',
          key: 'customerPhone',
          placeholder: '请输入至少4位客户手机号',
          input: {},
          check: (val) => {
            if (val.customerPhone.length < 4) {
              _this.$Message.warning('请输入至少4位客户手机号。')
            }
            return !(val.customerPhone.length < 4)
          }
        },
      }
    },
    {
      title: '认购房源',
      field: 'roomName',
      minWidth: 180,
      header: {
        filter: houseOpt.createRoom({
          key: 'roomId',
          title: '所属房源'
        })
      }
    },
    {
      title: '面积（㎡）',
      field: 'area',
      minWidth: 150,
    },
    {
      title: '渠道带看单',
      field: 'channelAttach',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: _this.resolveImgShow(params.row.channelAttach),
            color: _this.resolveDateShow(params.row.channelAttach) ? '#D9001B' :
              '#2d8cf0',
            event: 'preViewImg',
            data: [params.row, 'channelAttach', 'channelImages']
          })
        }
      }
    },
    {
      title: '网络获客凭证',
      field: 'networkCredentialsImages ',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: _this.resolveImgShow(params.row.networkCredentialsImages),
            color: _this.resolveDateShow(params.row.networkCredentialsImages) ?
              '#D9001B' : '#2d8cf0',
            event: 'preViewImg',
            data: [params.row, 'networkCredentialsImages',
              'networkCredentialsImages'
            ]
          })
        }
      }
    },
    {
      title: '认购单',
      field: 'subscriptionAttach',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: _this.resolveImgShow(params.row.subscriptionAttach),
            color: _this.resolveDateShow(params.row.subscriptionAttach) ?
              '#D9001B' : '#2d8cf0',
            event: 'preViewImg',
            data: [params.row, 'subscriptionAttach',
              'subscriptionImages'
            ]
          })
        }
      }
    }, {
      title: '认购收据及POS单',
      field: 'posAttach',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: _this.resolveImgShow(params.row.posAttach),
            color: _this.resolveDateShow(params.row.posAttach) ?
              '#D9001B' : '#2d8cf0',
            event: 'preViewImg',
            data: [params.row, 'posAttach',
              'subscriptionCreditImages'
            ]
          })
        }
      }
    },
    {
      field: 'subscriptionDate',
      minWidth: 150,
      title: '认购日期',
      header: {
        filter: dateOpt.range({
          title: '认购日期',
          startKey: 'subscriptionStartDate',
          endKey: 'subscriptionEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.subscriptionStartDate, val
                .subscriptionEndDate).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('认购日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.subscriptionStartDate, val
              .subscriptionEndDate).Y >= 2)
          }
        }),
      },
    },
    {
      field: 'productName',
      title: '所属产品',
      minWidth: 150,
      cell: {
        text: {
          lineClamp: 2,
          webkitBoxOrient: 'vertical',
          display: '-webkit-box',
          whiteSpace: 'inherit'
        }
      }
    },
    {
      title: '渠道名称',
      field: 'channelName',
      minWidth: 120,
      header: {
        filter: houseOpt.createChannel({
          key: 'channelId',
          title: '渠道名称'
        })
      }
    },
    {
      title: '合作类型',
      field: 'channelType',
      minWidth: 120,
      header: {
        filter: channelTypeMgr.create({
          key: 'cooperationType',
        }),
      },
      cell: {
        text: {
          type: 'dict',
          key: 'cooperationTypeFinal'
        }
      }
    },
    {
      field: 'channelCostValue',
      title: '渠道费用(元)',
      width: 130,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'channelFitName',
      title: '打法名称',
      width: 130,
      header: {
        filter: planOpt.create({
          key: 'planId',
          title: '打法名称',
          data:{
            to: (info) => {
              let channelIdList = []
              info.forEach(ele => {
                let channelId = ele.value.split('/')[1]
                channelIdList.push(channelId)
              });
              return {
                channelIdList: channelIdList.join(',').split(',')
              }
            }
          }
        })
      }
    },
    {
      field: 'contractorName',
      minWidth: 130,
      header: {
        filter: nameOpt.createContractor({
          key: 'contractorId',
          title: '认购人'
        })
      }
    },
    {
      field: 'handles',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: _this.unlocking('0104010300') ? '查看' : '',
            event: 'detailFun',
            data: params.row
          })

          btns.push({
            title: params.row.status == 2 && _this.unlocking('0104010201') &&
              params.row.isBreakLowPrice == 2 ? '审核' : '',
            event: 'detailFun',
            data: params.row
          })

          btns.push({
            title: _this.unlocking('0104010203') ? '下载附件' : '',
            event: 'exportAttachFun',
            data: params.row
          })

          return table.cell.btns(_this, h, btns)
        }
      }
    }]
  }

}

export default Column